#include <head.h>
int main(int argc, char const *argv[])
{
    int fd = open("111.txt", O_CREAT | O_RDWR, 0666);
    int fd_T = open("T.txt", O_CREAT | O_RDWR, 0666);
    // 设置随机数种子
    srand(time(NULL));

    // 生成一个0到99之间的随机数

    int i = 10;
    int T_num = 0;

    flock(fd, LOCK_EX);

    read(fd_T, &T_num, sizeof(int));

    lseek(fd, T_num, SEEK_SET);

    while (i--)
    {
        int random_number = rand() % 10;

        // 将随机数转换为字符串并存储在 buff 中
        char buff[32];
        snprintf(buff, sizeof(buff), "%d", random_number);

        sleep(1);
        write(fd, buff, strlen(buff));
    }
    T_num = lseek(fd, 0, SEEK_CUR);

    lseek(fd_T, 0, SEEK_SET);
    write(fd_T, &T_num, sizeof(int));

    flock(fd, LOCK_UN);

    close(fd);
    close(fd_T);

    return 0;
}